#define _WIN32_DCOM
#include <windows.h>
#include <iostream.h>
#include <stdio.h>
#include <io.h>
#include <fcntl.h>
#include "Component\component.h"

HRESULT HexStringToIP(char* output, REFIID riid, void** pObject)
{
	int max = strlen(output) / 2;
	char* pointer = (char*)CoTaskMemAlloc(max);

	for(int count = 0; count < max; count++)
	{
		//	0 - 9 (48 - 57)     a - f (97 - 102)
		int a1 = output[count*2];
		if(a1 > 47 && a1 < 58)
			a1 = output[count*2] - 48;
		else if(a1 > 96 && a1 < 103)
			a1 = output[count*2] - 87;

		int a2 = output[(count*2)+1];
		if(a2 > 47 && a2 < 58)
			a2 = output[(count*2)+1] - 48;
		else if(a2 > 96 && a2 < 103)
			a2 = output[(count*2)+1] - 87;

		pointer[count] = a1 * 16 + a2;
	}

	HRESULT hr;
	IStream* pStream = 0;
	hr = CreateStreamOnHGlobal(pointer, TRUE, &pStream);
	hr = CoUnmarshalInterface(pStream, riid, pObject);
	pStream->Release();
	return hr;
}

void main()
{
	CoInitialize(NULL);

	char output[1024];
	int fh = _open("c:\\ip.txt", _O_TEXT|_O_RDONLY);
	int read = _read(fh, output, 1024);
	_close(fh);
	output[read] = 0;

	printf("%s\n", output);
	ISum* pSum;
	HexStringToIP(output, IID_ISum, (void**)&pSum);

	int addition;
	pSum->Sum(4, 2, &addition);
	cout << "4 + 2 = " << addition << endl;

	pSum->Release();

	CoUninitialize();
}